/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package BD;

import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.JOptionPane;

/**
 *
 * @author Yalile Bermudes
 */
public class GestorProducto {
    
    private boolean exito;
    Conexion conexion;
    Image imagen;
    String codigo, nombre, descripcion, precio, fechaprecio, tipo, largo, ancho, color, material, empresafab, iva, udisponibles,
             categoria, descuento, fechadescuento;
    
    public GestorProducto(){
        conexion = new Conexion();
    }
    public boolean establecerConexion() {
        exito = conexion.conectar();
        return exito;
    }
    
    private Image convertirImagen(byte[] bytes) throws IOException {
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        Iterator readers = ImageIO.getImageReadersByFormatName("jpeg");
        ImageReader reader = (ImageReader) readers.next();
        Object source = bis;
        ImageInputStream iis = ImageIO.createImageInputStream(source);
        reader.setInput(iis, true);
        ImageReadParam param = reader.getDefaultReadParam();
        return reader.read(0, param);
    }
    
   public  boolean consultarProducto(String codigo){
        boolean existe = false;
        if(establecerConexion()){
            String consulta ="Select * From Producto where codigoproducto= '"+codigo+"';";
            try{
            ResultSet result = conexion.ejecutarConsulta(consulta);
            int cont=0;
            while(result.next()){
                codigo= result.getString(0);
                nombre=result.getString(1);
                try{
                imagen = convertirImagen(result.getBytes(2));
                }catch(IOException ex){
                    System.err.println(ex.getMessage());
                }
                empresafab =result.getString(3);
                descripcion=result.getString(4); 
                precio = result.getString(5);
                fechaprecio = result.getString(6);
                iva = result.getString(7);
                udisponibles =result.getString(8);
                tipo= result.getString(9);
                ancho = result.getString(10);
                largo= result.getString(11);
                color = result.getString(12);
                material = result.getString(13);
                categoria = result.getString(14);
                descuento = result.getNString(15);
                fechadescuento = result.getString(16);
                cont++;
            }
            if (cont > 0) {
                    existe = true;
                } else {
                    existe = false;
                }
            }catch(SQLException e){
                System.err.println("Exception: " + e.getMessage());
            }
        }else{
            JOptionPane.showMessageDialog(null, "No se pudo establecer conexion");
        }
        
        return existe;

    }
 public String getNombre(){
     return this.nombre;
 }
 
 public String getCodigo(){
     return this.codigo;
 }
 
 public Image getImagen(){
     return imagen;
 }
 
 public String getDescripcion(){
     return this.descripcion;
 }
 
 public String getPrecio(){
     return this.precio;
 }
 
 public String getTipo(){
     return this.tipo;
 }
 public String getLargo(){
     return this.largo;
 }
  
 public String getAncho(){
     return this.ancho;
 }
  
 public String getColor(){
     return this.color;
 }
  
 public String getMaterial(){
     return this.material;
 } 

    public String getDescuento() {
        return this.descuento;
    }

    public String getIva() {
        return this.iva;
    }

}
